package com.example.framework.register;

import com.example.framework.annotation.HelloScan;
import com.example.framework.bean.HelloFactoryBean;
import com.example.framework.scan.HelloClassPathBeanDefinitionScanner;
import com.example.service.HelloService;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Map;

public class HelloImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        Map<String, Object> annotationMap = importingClassMetadata.getAnnotationAttributes(HelloScan.class.getName());
        String basePackageName = (String) annotationMap.get("basePackageName");
        System.out.println(basePackageName);

        HelloClassPathBeanDefinitionScanner helloClassPathBeanDefinitionScanner = new HelloClassPathBeanDefinitionScanner(registry);
        helloClassPathBeanDefinitionScanner.addIncludeFilter((metadataReader, metadataReaderFactory) -> true);
        int scanCount = helloClassPathBeanDefinitionScanner.scan(basePackageName);
        System.out.println(scanCount);
    }
}
